package sy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.TsupplierOffer;
import sy.pageModel.DataGrid;
import sy.pageModel.SupplierOffer;
import sy.service.SupplierOfferServiceI;

@Service("supplierOfferService")
public class SupplierOfferServiceImpl implements SupplierOfferServiceI {

	private static final Logger logger = Logger.getLogger(SupplierOfferServiceImpl.class);

	private BaseDaoI<TsupplierOffer> supplierOfferDao;

	public BaseDaoI<TsupplierOffer> getVehicleInfoDao() {
		return getVehicleInfoDao();
	}

	@Autowired
	public void setSupplierOfferDao(BaseDaoI<TsupplierOffer> supplierOfferDao) {
		this.supplierOfferDao = supplierOfferDao;
	}

	
	public SupplierOffer save(SupplierOffer supplierOffer) {
		TsupplierOffer t = new TsupplierOffer();
		System.out.println("SupplierOffer**********save");
		BeanUtils.copyProperties(supplierOffer, t);
		t.setVehicleid(UUID.randomUUID().toString());
		//t.setCreatetime(new Date());
		supplierOfferDao.save(t);
		/*if (SupplierOffer.getRoleIds() != null) {
			String roleIds = "";
			String roleNames = "";
			for (String id : SupplierOffer.getRoleIds().split(",")) {
				Trole r = roleDao.get(Trole.class, id);
				if (r != null) {
					TvehicleInfoTrole ur = new TvehicleInfoTrole();
					ur.setId(UUID.randomUUID().toString());
					ur.setTrole(r);
					ur.setTvehicleInfo(t);
					vehicleInfoRoleDao.save(ur);

					if (roleIds.length() > 0) {
						roleIds += ",";
						roleNames += ",";
					}
					roleIds += r.getId();
					roleNames += r.getText();
				}
			}
			SupplierOffer.setRoleNames(roleNames);
			SupplierOffer.setRoleIds(roleIds);
		}*/
		BeanUtils.copyProperties(t, supplierOffer);
		return supplierOffer;
	}

	
	public SupplierOffer find(SupplierOffer supplierOffer) {
	/*	Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", SupplierOffer.getName());
		params.put("pwd", Encrypt.e(SupplierOffer.getPwd()));
		TvehicleInfo t = vehicleInfoDao.get("from TvehicleInfo t where t.name=:name and t.pwd=:pwd", params);
		if (t != null) {
			BeanUtils.copyProperties(t, SupplierOffer);

			String roleIds = "";
			String roleNames = "";
			String resourceIds = "";
			String resourceNames = "";
			List<String> resourceUrls = new ArrayList<String>();
			Set<TvehicleInfoTrole> ur = t.getTvehicleInfoTroles();// 获得当前用户的所有角色
			if (ur != null && !ur.isEmpty()) {
				for (TvehicleInfoTrole tur : ur) {
					if (roleIds.length() > 0) {
						roleIds += ",";
						roleNames += ",";
					}
					roleIds += tur.getTrole().getId();
					roleNames += tur.getTrole().getText();

					Set<TroleTresource> rr = tur.getTrole().getTroleTresources();// 获得当前角色所有资源
					if (rr != null && !rr.isEmpty()) {
						for (TroleTresource trr : rr) {
							if (resourceIds.length() > 0) {
								resourceIds += ",";
								resourceNames += ",";
							}
							resourceIds += trr.getTresource().getId();
							resourceNames += trr.getTresource().getText();
							resourceUrls.add(trr.getTresource().getUrl());
						}
					}
				}
			}
			SupplierOffer.setRoleIds(roleIds);
			SupplierOffer.setRoleNames(roleNames);
			SupplierOffer.setResourceIds(resourceIds);
			SupplierOffer.setResourceNames(resourceNames);
			SupplierOffer.setResourceUrls(resourceUrls);

			return SupplierOffer;
		}*/
		return null;
	}

	
	public DataGrid datagrid(SupplierOffer supplierOffer) {
		DataGrid dg = new DataGrid();
		String hql = "from TsupplierOffer t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(supplierOffer, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(supplierOffer, hql);
		List<TsupplierOffer> l = supplierOfferDao.find(hql, params, supplierOffer.getPage(), supplierOffer.getRows());
		List<SupplierOffer> nl = new ArrayList<SupplierOffer>();
		changeModel(l, nl);
		dg.setTotal(supplierOfferDao.count(totalHql, params));
		dg.setRows(nl);
		if(nl.size()>0){
		System.out.println("datagrid***********"+nl.get(0).getDescription()+"dddd");
		}
		return dg;
	}

	private void changeModel(List<TsupplierOffer> l, List<SupplierOffer> nl) {
		if (l != null && l.size() > 0) {
			for (TsupplierOffer t : l) {
				SupplierOffer supplierOffer = new SupplierOffer();
				BeanUtils.copyProperties(t, supplierOffer);

				/*Set<TvehicleInfo> s = t.getTvehicleInfo();
				if (s != null && !s.isEmpty()) {
					String roleIds = "";
					String roleNames = "";

					for (TvehicleInfoTrole tt : s) {
						if (roleIds.length() > 0) {
							roleIds += ",";
							roleNames += ",";
						}
						roleIds += tt.getTrole().getId();
						roleNames += tt.getTrole().getText();
					}

					u.setRoleIds(roleIds);
					u.setRoleNames(roleNames);
				}*/

				nl.add(supplierOffer);
			}
		}
	}

	private String addOrder(SupplierOffer supplierOffer, String hql) {
		if (supplierOffer.getSort() != null) {
			hql += " order by " + supplierOffer.getSort() + " " + supplierOffer.getOrder();
		}
		return hql;
	}

	private String addWhere(SupplierOffer supplierOffer, String hql, Map<String, Object> params) {
		hql += " where 1=1 ";
		/*if (SupplierOffer.getCreatedatetimeStart() != null) {
			hql += " and t.createdatetime >= :getCreatedatetimeStart";
			params.put("getCreatedatetimeStart", SupplierOffer.getCreatedatetimeStart());
		}
		if (SupplierOffer.getCreatedatetimeEnd() != null) {
			hql += " and t.createdatetime <= :getCreatedatetimeEnd";
			params.put("getCreatedatetimeEnd", SupplierOffer.getCreatedatetimeEnd());
		}
		if (SupplierOffer.getModifydatetimeStart() != null) {
			hql += " and t.modifydatetime >= :getModifydatetimeStart";
			params.put("getModifydatetimeStart", SupplierOffer.getModifydatetimeStart());
		}
		if (SupplierOffer.getModifydatetimeEnd() != null) {
			hql += " and t.modifydatetime <= :getModifydatetimeEnd";
			params.put("getModifydatetimeEnd", SupplierOffer.getModifydatetimeEnd());
		}
		if (SupplierOffer.getQ() != null && !SupplierOffer.getQ().trim().equals("")) {
			hql += " and t.name like :name ";
			params.put("name", "%%" + SupplierOffer.getQ().trim() + "%%");
		}
		if (SupplierOffer.getName() != null && !SupplierOffer.getName().trim().equals("")) {
			hql += " and t.name like :name";
			params.put("name", "%%" + SupplierOffer.getName().trim() + "%%");
		}*/
		return hql;
	}

	
	public List<SupplierOffer> combobox(SupplierOffer supplierOffer) {
		String hql = "from TvehicleInfo t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(supplierOffer, hql, params);
		hql += " order by name";
		List<TsupplierOffer> l = supplierOfferDao.find(hql, params, 1, 10);
		List<SupplierOffer> nl = new ArrayList<SupplierOffer>();
		changeModel(l, nl);
		return nl;
	}

	
	public void remove(String ids) {
		System.out.println("jin lai le "+ids);
		for (String id : ids.split(",")) {
			TsupplierOffer t = supplierOfferDao.get(TsupplierOffer.class, id);
			System.out.println(ids.length()+"**%%***");
			if (t != null) {
			    supplierOfferDao.delete(t);
				System.out.println("delete(t)&&&&&&&<<<<");
			}
		}
	}

	
	public SupplierOffer edit(SupplierOffer supplierOffer) {
		System.out.println("edit**SupplierOffer**");
		TsupplierOffer t = supplierOfferDao.get(TsupplierOffer.class, supplierOffer.getOfferid());
		if (t != null) {
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("tvehicleInfo", t);
			supplierOfferDao.saveOrUpdate(t);
			System.out.println("************1");
			BeanUtils.copyProperties(supplierOffer, t);
				System.out.println("****************2");
			BeanUtils.copyProperties(t, supplierOffer);

			return supplierOffer;
		}
		return null;
	}

}
